<template>
  <div class="progress-wrapper">
    <span class="time left">{{ formatTime(currentTime) }}</span>
    <div class="bar-wrapper">
      <div ref="bar" class="bar">
        <div class="bar-inner" @click="onBarClick">
          <div ref="passed" class="passed" :style="passedStyle"></div>
          <div
            class="bar-btn"
            :style="btnStyle"
            @touchstart.prevent="onTouchStart"
            @touchmove.prevent="onTouchMove"
            @touchend.prevent="onTouchEnd"
          ></div>
        </div>
      </div>
    </div>
    <span class="time right">{{ formatTime(duration) }}</span>
  </div>
</template>

<script>
import { formatTime } from '@/assets/js/utils'
import useDrag from './useDrag'
import { ref } from 'vue'
const BAR_BTN_WIDTH = 18
export default {
  name: 'Progress',
  props: {
    progress: {
      type: Number,
      default: 0
    },
    currentTime: {
      type: Number,
      default: 0
    },
    duration: {
      type: Number,
      default: 0
    }
  },
  emit: ['progress-changing', 'progress-changed'],
  setup(props, { emit }) {
    const offset = ref(0)
    const bar = ref(null)
    const passed = ref(null)
    const { onTouchStart, onTouchMove, onTouchEnd, onBarClick } = useDrag(
      bar,
      passed,
      emit,
      offset
    )
    return {
      offset,
      formatTime,
      onTouchStart,
      onTouchMove,
      onTouchEnd,
      onBarClick,
      bar,
      passed
    }
  },
  computed: {
    passedStyle() {
      return {
        width: `${this.offset + BAR_BTN_WIDTH / 2}px`
      }
    },

    btnStyle() {
      return {
        transform: `translate3d(${this.offset}px, -50%, 0)`
      }
    },

    fullScreen() {
      return this.$store.state.play.fullScreen
    }
  },

  watch: {
    progress(newProgress) {
      const bar = this.$refs.bar
      if (bar) {
        const moveWidth = bar.offsetWidth - BAR_BTN_WIDTH
        this.offset = moveWidth * newProgress
      }
    },

    fullScreen(newValue) {
      this.$nextTick(() => {
        const bar = this.$refs.bar
        if (bar) {
          const moveWidth = bar.offsetWidth - BAR_BTN_WIDTH
          this.offset = moveWidth * this.progress
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.progress-wrapper {
  display: flex;
  align-items: center;
  width: 80%;
  padding: 10px 0;
  margin: 0 auto;
  .time {
    width: 40px;
    color: #fff;
    font-size: 12px;
    line-height: 30px;
    &.left {
      text-align: left;
    }
    &.right {
      text-align: right;
    }
  }
  .bar-wrapper {
    flex: 1;
    .bar {
      display: flex;
      align-items: center;
      height: 30px;
      .bar-inner {
        position: relative;
        width: 100%;
        height: 4px;
        background: rgba(0, 0, 0, 0.3);
        .passed {
          position: absolute;
          left: 0;
          top: 0;
          width: 0;
          height: 100%;
          background-color: $color-list-title;
        }
        .bar-btn {
          position: absolute;
          left: 0;
          top: 50%;
          transform: translate3d(0, -50%, 0);
          width: 18px;
          height: 18px;
          border-radius: 50%;
          background-color: $color-list-title;
          &:after {
            content: '';
            position: absolute;
            left: 50%;
            top: 50%;
            display: block;
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background: #fff;
            transform: translate3d(-50%, -50%, 0);
          }
        }
      }
    }
  }
}
</style>
